<template>
  <div class="chart-box" ref="Charts"></div>
</template>

<script>
import resize from '@/components/mixins/resize'

export default {
  title: '圆环图',
  name: "EchartBar",
  mixins: [resize],
  props: {
    datas: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    datas: {
      deep: true,
      handler: function (val) {
        console.log(11, val)
        this.setOptions({data: val})
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$refs.Charts)
      this.setOptions({axis: [], data: []})
    },
    setOptions({axis = [], data = []}) {
      if (!this.chart) {
        return false
      }
      this.chart.setOption({
        color: ['#115091', '#1097fe', '#5870da', '#20c6fb', '#1CFEC3', '#FCFF00', '#00985F'],
        tooltip: {
          formatter: '{b}: {d}%',
          backgroundColor: "rgba(0,0,0,0.8)", //设置背景图片 rgba格式
          color: "white",
          borderWidth: "1", //边框宽度设置1
          borderColor: "gray", //设置边框颜色
          textStyle: {
            fontSize: 14,
            color: "#FFFFFF", //设置文字颜色
          },
        },
        legend: {
          top: '5%',
          left: 'center',
          show: false
        },
        series: [
          {
            name: '组件数量占比',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 0
            },
            label: {
              //设置每一块的名字是显示在图里面还是外面
              show: true,
              // position: "inside",
              textBorderColor: 'transparent',
              color:'#fff'
            },
            data: data
          }
        ]
      })
    }
  }
}
</script>

<style scoped lang="less">
  .chart-box {
    width: 100%;
    height: 100%;
  }
</style>